package com.community.process._1010.common.utils;


import com.community.process._1010.common.api.ErrorAssert;
import com.community.process._1010.domains.external.ExternalTokenLoginDTO;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CommonUtils {


    public static ExternalTokenLoginDTO getExternalTokenLoginDTO(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        String clientType = request.getHeader("Client-Type");
        String shopId = request.getHeader("Shop-Id");
        String platform = request.getHeader("Platform");
        ErrorAssert.trimHasLength(authHeader,"未检测到Authorization");
        ErrorAssert.trimHasLength(clientType,"缺少Client-Type参数");
        ErrorAssert.trimHasLength(clientType,"缺少Shop-Id参数");
        ErrorAssert.trimHasLength(platform,"缺少Platform参数");
        return new ExternalTokenLoginDTO(authHeader,clientType,shopId,platform);
    }

    public static List<Long> StringToLongArray(String data, boolean defaultValue){
        List<Long> list = new ArrayList<>();
        if(StringUtils.hasText(data)){
            String [] split = data.split(",");
            for (String s : split) {
                list.add(Long.parseLong(s));
            }
        }
        if(list.isEmpty() && defaultValue){
            list.add(-1L);
        }
        return list;
    }

    public static Long  getLoginId(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if(request.getAttribute("userId") == null){
            return 0L;
        }
        return Long.parseLong(request.getAttribute("userId").toString());
    }

    public static Long  getStoreId(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if(request.getAttribute("shopId") == null){
            return 0L;
        }
        return Long.parseLong(request.getAttribute("shopId").toString());
    }

    public static String  ClientType(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if(request.getAttribute("shopId") == null){
            return null;
        }
        return request.getAttribute("shopId").toString();
    }

    public static String  getAuthorization(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if(request.getAttribute("Authorization") == null){
            return null;
        }
        return request.getAttribute("Authorization").toString();
    }

    public static String getClientType() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if(request.getAttribute("Client-Type") == null){
            return null;
        }
        return request.getAttribute("Client-Type").toString();
    }

}
